package com.work.mdass.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.work.mdass.domain.Course;
import com.work.mdass.domain.TeachingMaterial;
import com.work.mdass.properties.CourseInterface;
import com.work.mdass.properties.TeachingMaterialInterface;

@Controller
@RequestMapping("/admin/mdass")
public class MdassController {
	
	@Autowired
	private CourseInterface courseInterface;
	@Autowired
	private TeachingMaterialInterface teachingMaterialInterface;
	
	/*
	 * 处理未申请、已申请、已征订事件
	 */
	@RequestMapping("/util")
	@ResponseBody
	public Map<Object,Object> mdassUtil(@RequestParam(required = false) String loginName,
			@RequestParam(required = false) String loginSerial,
			@RequestParam(required = false) Integer sign){//参数：教师名和教师工号
		
		List<Course>  courses = courseInterface.findByTName(loginName);
		
		List<TeachingMaterial> teachs = teachingMaterialInterface.findByTName(loginName);
		List<TeachingMaterial> teachsTemp = new ArrayList<>();
		
		List<Course> courseTemp_f = new ArrayList<>();//已申报
		
		List<Course> courseTemp_s = new ArrayList<>();//未申报
		
		
		
		if(teachs.size()!=0) {
			boolean falg;
			//去重
			for(int i=0;i<teachs.size();i++) {
				falg = false;
				for(int j = 0;j<teachsTemp.size();j++) {
					if(teachs.get(i).getCourseSerial().equals(teachsTemp.get(j).getCourseSerial())) {
						falg = true;
						break;
					}
				}
				if(!falg) {
					teachsTemp.add(teachs.get(i));
				}
			}
			
			//分别,获得已申报
			for(int i=0;i<courses.size();i++) {
				boolean falg_s = false;
				for(int j=0;j<teachsTemp.size();j++) {
					if(courses.get(i).getCourseSerial().equals(teachsTemp.get(j).getCourseSerial())) {
						courseTemp_f.add(courses.get(i));
						falg_s = true;
						break;
					}
				}
				if(!falg_s){
					courseTemp_s.add(courses.get(i));
				}
			}
		}else{//未申报时全部 courseTemp_s
			for(int i=0;i<courses.size();i++)
				courseTemp_s.add(courses.get(i));
		}
		Map<Object,Object> result = new HashMap<>();
		//sign=1代表已申报，sign=2代表未申报
		if(sign==2) {
			if(courseTemp_f.size()!=0){
				result.put("code", 0);
				result.put("msg", "success");
				result.put("data", courseTemp_f);
				result.put("count", courseTemp_f.size());
			}else {
				result.put("code", 1);
				result.put("msg", "暂时没有符合条件的数据");
				result.put("data", courseTemp_f);
				result.put("count", courseTemp_f.size());
			}
		}else if(sign==1) {
			if(courseTemp_s.size()!=0) {
				result.put("code", 0);
				result.put("msg", "success");
				result.put("data", courseTemp_s);
				result.put("count", courseTemp_s.size());
			}else {
				result.put("code", 1);
				result.put("msg", "暂时没有符合条件的数据");
				result.put("data", courseTemp_s);
				result.put("count", courseTemp_s.size());
			}
		}
		return result;
	}
	
}
